Introduction to Computer Vision¶


This notebook introduces some of the image processing algorithms/functions using the OpenCV library

Function Description
Read Image Read an image using cv.imread
plot_images Plots two images side by side
Convert RGB to grayscale Converts an RGB image to a grascale image
Convert RGB to HSV Converts an RGB image to an HSV image
Corner Detector Uses an algorithm called Harris corner detector to detect edges and corner
Normalize Normalize the values of the image
central_crop Crops the image with the given image dimensions around the center
BGR2RGB Converts a BGR image to an RGB image
resize_shortest_edge While maintaining the aspect ratio of the image, resizes the shortest edge

Aren't we doing computer vision throughout this bootcamp? Then why have a dedicated session for computer vision?¶

Computer Vision: Traditional method to teach computer understand images

CNNs: human inspired computing paradigm for computers to understand images

OpenCV ¶

OpenCV is an open source cross platform computer vision library. You will be using OpenCV to get familiar with some of the computer vision functions.

Import numpy, OpenCV and matplotlib to visualize images. We will use a mosaic image to demonstrate the effect of some vision operation to such image

In [ ]:
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt

How to read an image?¶

In [ ]:
img = cv.imread('img/mosaic.jpg')
plt.figure(figsize=(10, 10)), plt.axis("off"), plt.imshow(img);
print(img.shape)

How to plot images next to each other?¶

In [ ]:
def plot_images(original_image, processed_image):
    plt.figure(figsize=(15, 15))
    plt.subplot(121),plt.imshow(original_image),plt.title('Original')
    plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(processed_image),plt.title('Processed')
    plt.xticks([]), plt.yticks([])
    plt.show()

How to convert RGB image to Grayscale image?¶

A color image is represented on the RGB color space, however there are many different color spaces. Each of them have a particular purpose. We will explore some of them

A grayscale image has only one channel which represents the amount of light that each pixel contains. One of the main purposes of grayscale images on vision applications is to detect edges on images.

In [ ]:
gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
# plt.figure(figsize=(10, 10)), plt.axis("off"), plt.imshow(gray, cmap='gray');
plot_images(img, gray)

How to convert RGB image to HSV format?¶

HSV color space represents an image using the channels hue, saturation and value. This color space aligns a bit better to the way human perceives color-making attributes, and in vision application this color space is useful to detect color more accurately.

In [ ]:
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
plot_images(img, hsv)

Corner Detector¶

Harris Corner Detector¶

The Harris Corner Detector is primarily designed for detecting corner points or keypoints in images and not specifically meant for detecting edges. While it can potentially detect some edges, its primary focus is on identifying corners in the image.

In [ ]:
gray = np.float32(cv.cvtColor(img,cv.COLOR_BGR2GRAY))
harris = cv.cornerHarris(gray,2,3,0.1)
plt.figure(figsize=(10, 10)), plt.axis("off"), plt.imshow(harris, cmap='gray');

How to normalize image?¶

Normalize the image by dividing by 256, then subtracting the mean i.e. 0.5, and then amplifying the values by multiplying by 2.

In [ ]:
def normalize(image):
    image_norm=image/256.0
    image_norm=image_norm-0.5
    image_norm=image_norm*2
    plot_images(image, image_norm)
    return image_norm
In [ ]:
norm_image = normalize(img)

How to central crop image?¶

Reduce the size of the image around the center.

In [ ]:
def central_crop(image, crop_height, crop_width):
    image_height = image.shape[0]
    image_width = image.shape[1]
    offset_height = (image_height - crop_height) // 2
    offset_width = (image_width - crop_width) // 2
    image_crop = image[offset_height:offset_height + crop_height, offset_width:
                 offset_width + crop_width, :]
    plot_images(image, image_crop)
    return image_crop
In [ ]:
cropped_image = central_crop(img, 213, 320)
print("Size of original image: ", img.shape)
print("Size of cropped image: ", cropped_image.shape)

How to convert BGR image to RGB image?¶

In [ ]:
def BGR2RGB(image):
    B, G, R = cv.split(image)
    processed_image = cv.merge([R, G, B])
    plot_images(image, processed_image)
    return processed_image
In [ ]:
rgb_image = BGR2RGB(img)

How to resize the shortest edge of an image?¶

In [ ]:
def resize_shortest_edge(image, size):
    H, W = image.shape[:2]
    if H >= W:
        nW = size
        nH = int(float(H)/W * size)
    else:
        nH = size
        nW = int(float(W)/H * size)
    processed_image = cv.resize(image,(nW,nH))
    plot_images(image, processed_image)

    return processed_image
In [ ]:
processed_image = resize_shortest_edge(img, 200)
print("Size of original image: ", img.shape)
print("Size of processed image: ", processed_image.shape)